<?php if(!defined('WDK_PATH')) exit('No direct script access allowed');
/**
 * @since 2011. 10. 08
 * @author kinuz
 */
/**
 * 	48(0) ~ 57(9) : 10
 *	65(A) ~ 90(Z) : 26
 * 	97(a) ~ 122(z) : 26
 **/
define("__CATEGORY_TYPE_NUMBER__", 		bindec('1'));
define("__CATEGORY_TYPE_CAPITAL__", 	bindec('10'));
define("__CATEGORY_TYPE_SMALL_LETTER__", bindec('100'));
define("CATEGORY_NUMBER_ONLY",			__CATEGORY_TYPE_NUMBER__);	//	1
define("CATEGORY_CAPITAL_ONLY",			__CATEGORY_TYPE_CAPITAL__);	//	2
define("CATEGORY_CAPITAL_WITH_NUMBER",	__CATEGORY_TYPE_NUMBER__+__CATEGORY_TYPE_CAPITAL__);	//	3
define("CATEGORY_ALPHARBET",			__CATEGORY_TYPE_CAPITAL__+__CATEGORY_TYPE_SMALL_LETTER__);	//	6
define("CATEGORY_ALL",					__CATEGORY_TYPE_NUMBER__+__CATEGORY_TYPE_CAPITAL__+__CATEGORY_TYPE_SMALL_LETTER__);	//	7
class Hp_Category
{
	private $idx;
	private $length=2;
	public function init($mode)
	{
		$this->idx = array();
		if($mode & __CATEGORY_TYPE_NUMBER__)		{for($i=ord('0'); $i <=ord('9'); $i++){array_push($this->idx, chr($i));}}
		if($mode & __CATEGORY_TYPE_CAPITAL__)		{for($i=ord('A'); $i <=ord('Z'); $i++){array_push($this->idx, chr($i));}}
		if($mode & __CATEGORY_TYPE_SMALL_LETTER__)	{for($i=ord('a'); $i <=ord('z'); $i++){array_push($this->idx, chr($i));}}
	}
	public function setLength($length)
	{
		$this->length = $length;
	}
	public function next($category, $mode = CATEGORY_CAPITAL_ONLY)
	{
		if($this->idx == null)
		{
			$this->init($mode);
		}
		switch($this->length)
		{
			case 2:
				if(!$category) return $this->_nextChar("").$this->_nextChar("");
				list($first, $second) = str_split($category);
				if(($second = $this->_nextChar($second)) === false)
				{
					if(($first = $this->_nextChar($first)) === false)
					{
						return "ERROR - no more category";
					}
					$second = $this->_nextChar("");
				}
				return $first.$second;
				break;
			case 3:
				if(!$category) return $this->_nextChar("").$this->_nextChar("").$this->_nextChar("");
				list($first, $second, $third) = str_split($category);
				if(($third = $this->_nextChar($third)) === false)
				{
					if(($second = $this->_nextChar($second)) === false)
					{
						if(($first = $this->_nextChar($first)) === false)
						{
							return "ERROR - no more category";
						}
						$second = $this->_nextChar("");
					}
					$third = $this->_nextChar("");
				}
				return $first.$second.$third;
				
				break;
			default:
				if(($category = $this->_nextChar($category)) === false)
				{
					return "ERROR - no more category";
				}
				return $category;	
				break;
		}
	}

	public function prev($category, $mode = CATEGORY_CAPITAL_WITH_NUMBER)
	{
		if($this->idx == null)
		{
			$this->init($mode);
		}
		if($this->length == 1)
		{
			if(($category = $this->_prevChar($category)) === false)
			{
				return "ERROR - first category";
			}
			return $category;
		}
		else
		{
			if(!$category) return "ERROR - first category";
			$first = substr($category, 0, 1);
			$second = substr($category, 1, 1);
			if(($second = $this->_prevChar($second)) === false)
			{
				if(($first = $this->_prevChar($first)) === false)
				{
					return "ERROR - first category";
				}
				$second = $this->_prevChar("");
			}
			return $first.$second;
		}
	}
	private function _nextChar($char)
	{
		if($char == "") return $this->idx[0];
		$idx = array_search($char, $this->idx)+1;
		if(isset($this->idx[$idx]) && $this->idx[$idx]!==false) return $this->idx[$idx];
		return false;
	}
	
	private function _prevChar($char)
	{
		if($char == "") return $this->idx[count($this->idx)-1];
		$idx = array_search($char, $this->idx);
		if($idx === 0) return false;
		return $this->idx[$idx-1];
	}
}